package regression;

import java.util.Date;

import main.DataConnection;

/**
 * Generic Regression class for every problem (specified in constructor) based on car characteristic.
 * 
 * @author Dawid Nejman
 * @version 1.0 21-12-2011
 */
public class ProblemRegression {

	private String problemName = "";
	private String subproblemName = "";

	/**
	 * Constructor to make regression object for a problem with {@code problemName} specified in argument. {@code problemName} must be valid name which is in
	 * the database. Otherwise an exception is thrown.
	 * 
	 * @param problemName
	 *            valid name of the problem.
	 * @throws Throwable
	 *             excetpion thrown if there is no such problem in the database.
	 */
	public ProblemRegression(String problemName) throws Throwable {
		if (DataConnection.problemExists(problemName))
			this.problemName = problemName;
		else
			throw new Throwable("No such problem defined!");
	}

	/**
	 * Constructor to make regression object for a problem with {@code problemName} and {@code subproblemName} specified in argument. {@code problemName}
	 * {@code subproblemName} must be valid name which is in the database. Otherwise an exception is thrown.
	 * 
	 * @param problemName
	 *            valid name of the problem.
	 * @param subproblemName
	 *            valid name of the subproblem.
	 * @throws Throwable
	 *             excetpion thrown if there is no such problem and / or subproblem in the database.
	 */
	public ProblemRegression(String problemName, String subproblemName) throws Throwable {
		if (DataConnection.problemAndSubproblemExists(problemName, subproblemName)) {
			this.problemName = problemName;
			this.subproblemName = subproblemName;
		} else
			throw new Throwable("No such problem defined!");
	}

	/**
	 * Returns value estimation for a problem in a given {@code date}. Value is evaluated using car regression and characteristic received from database
	 * procedure.
	 * 
	 * @param date
	 *            time for which we want to get estimate.
	 * @return double estimate value.
	 */
	public double getNumberFor(Date date) {
		IRegression cr = CarRegression.getInstance(DataConnection.getCarsData());
		double characteristic = 0.0;
		double carFutureValue = 0.0;

		if (subproblemName.isEmpty())
			characteristic = DataConnection.getProblemCarCharacteristic(this.problemName);
		else
			characteristic = DataConnection.getProblemCarCharacteristic(this.problemName, this.subproblemName);

		carFutureValue = cr.getNumberFor(date);
		return carFutureValue * characteristic;
	}
}